<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends AdminController {
    public function index(){
        // var_dump(I('post'));

        $Order = M('Order'); // 实例化Order对象
        $count = $Order->count();// 查询满足要求的总记录数
        $num = 7;
        $show = cjl_page($count,$num);
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        // $data = $Order->order('u_time desc , id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        // alias() 将表重命名
        // __CATEGORY__ ==》 表前缀+表 tp_category
        // __Order_INFO__ ==》 表前缀+表 tp_Order_info
        // $data = $Order
  //               ->alias('g')
  //               ->order('g.u_time desc , g.id desc')
  //               ->page(I('get.p',1).','.$n)
  //               ->select();

        $data = M('Order')
                ->alias('o')
                ->field('o.*,g.img as img,g.title as title,o.pay_type,o.num,o.shop_price,u.username as username,u.mobile as mobile,u.email as email')
                ->join('__GOODS__ as g on o.goods_id = g.id','left')
                ->join('__USER__ as u on o.user_id = u.id','left')
                ->order('u_time desc,id desc')
                ->where('o.status > 0')
                ->page(I('get.p',1).','.$num)
                ->select();
        // $this->order = $order;
        // var_dump($data);
        // var_dump($Order->_sql());
        $this->assign('data',$data);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('cjl_title','订单列表');// 赋值分页输出
        // var_dump($data);exit;
       
        $this->display();
    }

    public function delete($id=0)
    {
        // var_dump($id);exit;
        if($id < 1){
            $this->error('参数错误,至少选择一条数据');
        }
        if(!is_array($id)){
            $res = M('Order')->delete($id);
        }else{
            $wh['id'] = array('in',implode(',', $id));
            // $wh = 'id in ('.implode(',', $id).')';
            $res = M('Order')->where($wh)->delete(); 
        }
        // var_dump($wh);
        // var_dump($res);
        // var_dump(M('Order')->_sql());exit;
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败',U('index'));            
        }
    }
    
   
    public function edit($id=0){
        if ($id < 1) {
            $this->error('参数错误');
            }
        
        // var_dump($id);exit;
        //说明修改
        // $one = M('About')->find($id);

        $order = M('Order')->find($id);

        // var_dump($order);
        $user_id = $order['user_id'];
        $goods_id = $order['goods_id'];
        $address_id =$order['address_id'];
        $products_id = $order['products_id'];
        $goods = M('Goods')->field('id,title,img')->where('id = '.$goods_id)->find();
        $user = M('User')->where('id = '.$user_id)->find();
        $address = M('Address')->where('id = '.$address_id)->find();
        // $products = M('Products')->field('id','attr_array')->where('id = '.$products_id)->find();
        // var_dump($goods);exit;
        // var_dump($user);
        // var_dump($address);exit;
        // var_dump($products);exit;
        $Products = M('Products')->where('id = '.$products_id)->order('id')->select();
            $this->assign('Products',$Products);
            $attr = M('Attr')
                    ->order('u_time desc,id desc')
                    ->select();
            $this->assign('attr',$attr);

        $this->order = $order;
        $this->goods = $goods;
        $this->user = $user;
        $this->address = $address;
        // $this->products = $products;
        //==> $this->assign('one',$one);
        $this->assign('cjl_title','订单详情');// 赋值数据集
        $this->display();
    }    
}